indexApp
    .controller(
        'predictData',
        function($scope, $http, $timeout,$interval) {

            //$scope.url = "http://119.167.221.16:23000/";
            $scope.url = "http://127.0.0.1:3000/";
            function getURLParameters(){
                var url=decodeURI(location.href);
                var paramArr = url.substring(url.indexOf("?")+1,url.length).split("&");
                var params = {};
                for(var i = 0 ; i < paramArr.length; i ++){
                    var a=paramArr[i].split('=');
                    params[a[0]] = a[1];
                }
                console.log("params",params);
                return params;
            }
            $scope.urlParams = getURLParameters();



            $scope.taskList=[];
            $scope.currentMySQLTable={
                name:'modelRecord',
                database:'FundingModel'
            };
            $scope.loadModel={
                modelName:null,
                tableName:null,
                oldTableName:null,
                uid:null,
                modelUid:null
            };
            $scope.currentMySQLTablePre={
                name:null,
                database:"predictDataSet"
            };
            $scope.taskOuputDivVisible=false;
            $scope.tableItemPre=[];
            $scope.mysqlTableSampleDataPre=[];
            $scope.countItemPre=[];
            $scope.taskConf=null;
            $scope.preCountList=[];
            $scope.selected=null;
            $scope.getAllTasks=function () {
                $scope.mysqlTableSampleData=[];
                $scope.itemType={};
                $scope.countItem=[];
                $http({
                    method: 'post',
                    url: $scope.url + 'gettask',
                    data:$scope.currentMySQLTable
                }).then(function (resp, status) {
                    $scope.taskList=resp.data.res;
                    console.log($scope.taskList);
                    $scope.status = status;
                }, function (resp, status) {
                    $scope.resp = resp;
                    $scope.status = status;
                });
            };
            $scope.selectTask=function (task) {
                $scope.taskConf=task[8];
                $scope.loadModel.modelName=task[3];
                $scope.loadModel.modelUid=task[1];
                console.log($scope.taskConf);
            };
            $scope.labelDes=null;
            var labelList=[];
            var dataList=[];
            $scope.predictLable=function () {
                var url=decodeURI(location.href);
                console.log(url);
                //$scope.loadModel.tableName=url.substring(url.indexOf("?")+11,url.indexOf("&"));
                //$scope.loadModel.oldTableName=url.substring(url.indexOf("&")+14,url.indexOf("@"));
                //$scope.loadModel.uid=url.substring(url.indexOf("@")+8,url.length);
                $scope.loadModel.tableName=$scope.urlParams.tablename;
                $scope.loadModel.oldTableName=$scope.urlParams.oldtablename
                $scope.loadModel.uid=$scope.urlParams.taskId;
                console.log($scope.loadModel.tableName);
                $http({
                    method: 'post',
                    url: $scope.url + 'loadmodel',
                    data:$scope.loadModel
                }).then(function (resp, status) {
                    $scope.preCountList=resp.data.countLabel;
                    $scope.labelDes=resp.data.labelDes;
                    for(var key in $scope.labelDes){
                        chartMap={};
                        labelList.push($scope.labelDes[key]);
                        chartMap.value=$scope.preCountList[key];
                        chartMap.name=$scope.labelDes[key];
                        dataList.push(chartMap);
                    }
                    console.log("dataList",dataList);
                    console.log("return",resp.data);
                    console.log("labelList",labelList);
                    $scope.currentMySQLTablePre.name=resp.data.predictTableName;
                    $scope.getPreDataSample();
                    $scope.get3dchart();
                    $scope.taskOuputDivVisible=true;
                    swal({
                        title: '完成！',
                        timer: 2000
                    });
                    console.log($scope.preCountList);
                    $scope.status = status;
                }, function (resp, status) {
                    $scope.resp = resp;
                    $scope.status = status;
                });
            };
            $scope.getPreDataSample=function () {
                $scope.tableItemPre=[];
                $scope.mysqlTableSampleDataPre=[];
                $scope.itemTypePre={};
                $scope.countItemPre=[];
                $http({
                    method: 'post',
                    url: $scope.url + 'gettableItem',
                    data:$scope.currentMySQLTablePre
                }).then(function (resp, status) {
                    $scope.tableItemPre=resp.data.res;
                    console.log($scope.tableItemPre);
                    for (var i = 0; i<$scope.tableItemPre.length; i++) {
                        $scope.countItemPre.push(i.toString());
                    }
                    $scope.status = status;
                }, function (resp, status) {
                    $scope.resp = resp;
                    $scope.status = status;
                });
                $http({
                    method: 'post',
                    url: $scope.url + 'getsortsampledata',
                    data:$scope.currentMySQLTablePre
                }).then(function (resp, status) {
                    $scope.mysqlTableSampleDataPre=resp.data.res;
                    console.log($scope.mysqlTableSampleDataPre);
                    $scope.status = status;
                }, function (resp, status) {
                    $scope.resp = resp;
                    $scope.status = status;
                });
            };
            $scope.get3dchart=function () {
                var myChart = echarts.init(document.getElementById('3DChart'));
                console.log(myChart);
                myChart.setOption({
                    title : {
                        text: '预测标签图',
                        subtext: '依据分类模型预测',
                        x:'center'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        x : 'center',
                        y : 'bottom',
                        data:labelList
                    },
                    toolbox: {
                        show : true,
                        feature : {
                            mark : {show: true},
                            dataView : {show: true, readOnly: false},
                            magicType : {
                                show: true,
                                type: ['pie', 'funnel']
                            },
                            restore : {show: true},
                            saveAsImage : {show: true}
                        }
                    },
                    calculable : true,
                    series : [
                        {
                            name:'半径模式',
                            type:'pie',
                            radius : [20, 110],
                            center : ['25%', 200],
                            roseType : 'radius',
                            width: '40%',       // for funnel
                            max: 40,            // for funnel
                            itemStyle : {
                                normal : {
                                    label : {
                                        show : false
                                    },
                                    labelLine : {
                                        show : false
                                    }
                                },
                                emphasis : {
                                    label : {
                                        show : true
                                    },
                                    labelLine : {
                                        show : true
                                    }
                                }
                            },
                            data:dataList
                            /*[
                                {value:$scope.preCountList[0], name:'亏损百亿'},
                                {value:$scope.preCountList[1], name:'亏损十亿'},
                                {value:$scope.preCountList[2], name:'亏损亿'},
                                {value:$scope.preCountList[3], name:'亏损千万'},
                                {value:$scope.preCountList[4], name:'亏损百万'},
                                {value:$scope.preCountList[5], name:'亏损万'},
                                {value:$scope.preCountList[7], name:'盈利万'},
                                {value:$scope.preCountList[8], name:'盈利百万'},
                                {value:$scope.preCountList[9], name:'盈利千万'},
                                {value:$scope.preCountList[10], name:'盈利亿'},
                                {value:$scope.preCountList[11], name:'盈利十亿'},
                                {value:$scope.preCountList[12], name:'盈利百亿'}
                            ]*/
                        },
                        {
                            name:'面积模式',
                            type:'pie',
                            radius : [30, 110],
                            center : ['75%', 200],
                            roseType : 'area',
                            x: '50%',               // for funnel
                            max: 40,                // for funnel
                            sort : 'ascending',     // for funnel
                            data:dataList
                        }
                    ]
                },true);

            }
        });